extends Node2D

# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var efftool = preload("res://combat/utils/effect_util.gd")
onready var world = $World

var query: QueryManager
var combat


func _init():
	load_config()


# Called when the node enters the scene tree for the first time.
func _ready():
	query = world.query_manager
	combat = Combat.new(world)
	#set the combat system lib data
	combat.set_lib(Res.cfgData.get("SkillModel"), Res.cfgData.get("BuffModel"))
	world.continue_init()  #resume world init, must be called
	yield(get_tree().create_timer(1.0), "timeout")
	round_start()


func round_start():
	print("round start")
	var globalRound = query.fetch_single("GlobalRound").data as GlobalRound
	globalRound.count = 1

	var single = query.fetch_single("GlobalReady")
	if not single:
		return print("no single")

	var globalReady: GlobalReady = single.data as GlobalReady
	#globalReady._round = 1

	# var list = []
	# for i in range(100):
	# 	list.append(next_int(1, 20))
	# print("----rand---", list)
	# list.resize(10)
	# print("----rand--- resize", list)
	# var addCmd = query.fetch_single("AddCmd").data as AddCmd
	# addCmd.value = []


func next_int(start, end):
	return randi() % end + start


func load_config():
	var files = ["skill_model", "buff_model"]

	for file in files:
		var path = "res://example/data/%s.csv" % file
		var csv = CSVFile.new()
		csv.load_file(path)
		var headers = csv._headers
		var rows = csv._array.get_data()
		var models = {}
		for row in rows:
			var model = ConfigModel.new(csv._model_name)
			for key in headers:
				model.set(key, row[headers[key]])
				models[model.get("id")] = model
		Res.cfgData[csv._model_name] = models
